package com.lottery.ruok.lottrey.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.OfficalLotteryDetailActivity;
import com.lottery.ruok.lottrey.adapter.LotteryChangeTypeAdapter2;
import com.lottery.ruok.lottrey.adapter.LotteryChangeTypeAdapter3;
import com.lottery.ruok.lottrey.bean.OfficalType;
import com.lottery.ruok.lottrey.eventbusObject.ChangePlayType;
import com.lottery.ruok.lottrey.retrofitModule.response.GetLotteryDetailResponse;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LotteryChangeTypeFragment extends BaseFragment {
//    @BindView(R.id.type1)
//    ListView type1;
    @BindView(R.id.type2)
    ListView type2;
    @BindView(R.id.type3)
    ListView type3;
    @BindView(R.id.close)
    ImageView close;

//    private LotteryChangeTypeAdapter1 adapter1;
    private GetLotteryDetailResponse getLotteryDetailResponse;
    private ArrayList<OfficalType> officalTypes;
    private int type2Position;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_lottery_change_type, null);

        ButterKnife.bind(this, view);

        init();

        return view;
    }

    private void init() {
//        adapter1 = new LotteryChangeTypeAdapter1(getActivity(), getLotteryDetailResponse);
//        type1.setAdapter(adapter1);

        LotteryChangeTypeAdapter2 adapter2 = new LotteryChangeTypeAdapter2(getActivity(), officalTypes);
        type2.setAdapter(adapter2);
        type2.setSelection(type2Position);

        LotteryChangeTypeAdapter3 lotteryChangeTypeAdapter3 = new LotteryChangeTypeAdapter3(getActivity(), getLotteryDetailResponse);
        type3.setAdapter(lotteryChangeTypeAdapter3);

//        type1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                ((OfficalLotteryDetailActivity) getActivity()).typePosition1 = i;
//
//                ((OfficalLotteryDetailActivity) getActivity()).typePosition2 = 0;
//
//                ((BaseAdapter) (type1.getAdapter())).notifyDataSetChanged();
//
//                LotteryChangeTypeAdapter2 adapter2 = new LotteryChangeTypeAdapter2(getActivity(), getLotteryDetailResponse);
//                type2.setAdapter(adapter2);
//
//                LotteryChangeTypeAdapter3 lotteryChangeTypeAdapter3 = new LotteryChangeTypeAdapter3(getActivity(), getLotteryDetailResponse);
//                type3.setAdapter(lotteryChangeTypeAdapter3);
//            }
//        });

        type2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                ((OfficalLotteryDetailActivity) getActivity()).typePosition2 = i;
                if(getActivity() != null){
                    int temp = 0;
                    if (getLotteryDetailResponse == null) {
                        return;
                    }
                    labelB:
                    for (int p = 0; p < getLotteryDetailResponse.getLayouts().size(); p++) {
                        for (int j = 0; j < getLotteryDetailResponse.getLayouts().get(p).getChildren().size(); j++) {
                            if(temp == i){
                                ((OfficalLotteryDetailActivity) getActivity()).typePosition1 = p;
                                ((OfficalLotteryDetailActivity) getActivity()).typePosition2 = j;
                                break labelB;
                            }
                            temp++;
                        }
                    }


                    for(OfficalType officalType: officalTypes){
                        officalType.setChoose(false);
                    }
                    officalTypes.get(i).setChoose(true);

                    ((BaseAdapter) (type2.getAdapter())).notifyDataSetChanged();

                    LotteryChangeTypeAdapter3 lotteryChangeTypeAdapter3 = new LotteryChangeTypeAdapter3(getActivity(), getLotteryDetailResponse);
                    type3.setAdapter(lotteryChangeTypeAdapter3);
                }
            }
        });

        type3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int t, long l) {
                if(getActivity() != null) {
                    if (getLotteryDetailResponse == null) {
                        return;
                    }
                    labelA:
                    for (int i = 0; i < getLotteryDetailResponse.getLayouts().size(); i++) {
                        if (getLotteryDetailResponse.getLayouts().get(i).getSelecteds().equals("1")) {
                            getLotteryDetailResponse.getLayouts().get(i).setSelecteds("0");
                            for (int j = 0; j < getLotteryDetailResponse.getLayouts().get(i).getChildren().size(); j++) {
                                if (getLotteryDetailResponse.getLayouts().get(i).getChildren().get(j).getSelecteds().equals("1")) {
                                    getLotteryDetailResponse.getLayouts().get(i).getChildren().get(j).setSelecteds("0");
                                    for (int m = 0; m < getLotteryDetailResponse.getLayouts().get(i).getChildren().get(j).getChildren().size(); m++) {
                                        if (getLotteryDetailResponse.getLayouts().get(i).getChildren().get(j).getChildren().get(m).getSelecteds().equals("1")) {
                                            getLotteryDetailResponse.getLayouts().get(i).getChildren().get(j).getChildren().get(m).setSelecteds("0");
                                            break labelA;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    getLotteryDetailResponse.getLayouts().get(((OfficalLotteryDetailActivity) getActivity()).typePosition1).setSelecteds("1");
                    getLotteryDetailResponse.getLayouts().get(((OfficalLotteryDetailActivity) getActivity()).typePosition1).getChildren().get(((OfficalLotteryDetailActivity) getActivity()).typePosition2).setSelecteds("1");
                    getLotteryDetailResponse.getLayouts().get(((OfficalLotteryDetailActivity) getActivity()).typePosition1).getChildren().get(((OfficalLotteryDetailActivity) getActivity()).typePosition2).getChildren().get(t).setSelecteds("1");

                    ((OfficalLotteryDetailActivity) getActivity()).typePosition3 = t;
                    if (getLotteryDetailResponse.getLayouts().get(((OfficalLotteryDetailActivity) getActivity()).typePosition1).getChildren().get(((OfficalLotteryDetailActivity) getActivity()).typePosition2).getChildren().get(t).getName().equals("组三")) {
                        EventBus.getDefault().post(new ChangePlayType(1));
                    } else {
                        EventBus.getDefault().post(new ChangePlayType(0));
                    }
                    try {
                        getActivity().getSupportFragmentManager().popBackStack();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public void setDatas(GetLotteryDetailResponse s) {
        getLotteryDetailResponse = s;
        officalTypes = new ArrayList<>();
        if(getLotteryDetailResponse != null){
            for (int i = 0; i < getLotteryDetailResponse.getLayouts().size(); i++) {
                for (int j = 0; j < getLotteryDetailResponse.getLayouts().get(i).getChildren().size(); j++) {
                    boolean isChoose = false;
                    if(getLotteryDetailResponse.getLayouts().get(i).getSelecteds().equals("1") && getLotteryDetailResponse.getLayouts().get(i).getChildren().get(j).getSelecteds().equals("1")){
                        type2Position = officalTypes.size();
                        isChoose = true;
                    }
                    OfficalType officalType = new OfficalType(getLotteryDetailResponse.getLayouts().get(i).getChildren().get(j).getName(), isChoose);
                    officalTypes.add(officalType);
                }
            }
        }
    }

    @OnClick(R.id.close)
    public void onViewClicked() {
        try {
            getActivity().getSupportFragmentManager().popBackStack();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}